package com.flyco.modules.cms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.cms.entity.CmsArticleV2;
import com.flyco.modules.cms.entity.CmsArticleV2Comment;
import com.flyco.modules.cms.entity.CmsArticleV2CommentReply;
import com.flyco.modules.cms.service.ICmsArticleV2CommentReplyService;
import com.flyco.modules.cms.service.ICmsArticleV2CommentService;
import com.flyco.modules.cms.service.ICmsArticleV2Service;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 文章表
 * @Author: flyco
 * @Date: 2020-07-16
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "文章表")
@RestController
@RequestMapping("/f/cms/cmsArticleV2")
public class CmsArticleV2Controller extends BaseController<CmsArticleV2, ICmsArticleV2Service> {
    @Autowired
    private ICmsArticleV2Service cmsArticleV2Service;
    @Autowired
    private ICmsArticleV2CommentService cmsArticleV2CommentService;
    @Autowired
    private ICmsArticleV2CommentReplyService cmsArticleV2CommentReplyService;

    /**
     * 分页列表查询
     *
     * @param cmsArticleV2
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "文章表-分页列表查询")
    @ApiOperation(value = "文章表-分页列表查询", notes = "文章表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CmsArticleV2 cmsArticleV2,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CmsArticleV2> queryWrapper = QueryGenerator.initQueryWrapper(cmsArticleV2, req.getParameterMap());
        Page<CmsArticleV2> page = new Page<CmsArticleV2>(pageNo, pageSize);
        IPage<CmsArticleV2> pageList = cmsArticleV2Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param cmsArticleV2
     * @return
     */
    @AutoLog(value = "文章表-添加")
    @ApiOperation(value = "文章表-添加", notes = "文章表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CmsArticleV2 cmsArticleV2) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cmsArticleV2.setPublisher(sysUser.getUsername());
        cmsArticleV2.setStatus(1);
        cmsArticleV2Service.save(cmsArticleV2);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cmsArticleV2
     * @return
     */
    @AutoLog(value = "文章表-编辑")
    @ApiOperation(value = "文章表-编辑", notes = "文章表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CmsArticleV2 cmsArticleV2) {
        cmsArticleV2Service.updateById(cmsArticleV2);
        return Result.ok("编辑成功!");
    }

    /**
     * 发布
     *
     * @param cmsArticleV2
     * @return
     */
    @AutoLog(value = "文章表-发布")
    @ApiOperation(value = "文章表-发布", notes = "文章表-编辑")
    @PutMapping(value = "/publish")
    public Result<?> publish(@RequestBody CmsArticleV2 cmsArticleV2) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CmsArticleV2 updateArticleV2 = new CmsArticleV2();
        updateArticleV2.setId(cmsArticleV2.getId());
        updateArticleV2.setStatus(2);
        updateArticleV2.setPublisher(sysUser.getUsername());
        cmsArticleV2Service.updateById(updateArticleV2);
        return Result.ok("编辑成功!");
    }


    /**
     * 审核
     *
     * @param cmsArticleV2
     * @return
     */
    @AutoLog(value = "文章表-审核")
    @ApiOperation(value = "文章表-审核", notes = "文章表-审核")
    @PutMapping(value = "/review")
    public Result<?> review(@RequestBody CmsArticleV2 cmsArticleV2) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CmsArticleV2 updateArticleV2 = new CmsArticleV2();
        updateArticleV2.setId(cmsArticleV2.getId());
        updateArticleV2.setStatus(cmsArticleV2.getStatus());
        updateArticleV2.setReviewer(sysUser.getUsername());
        cmsArticleV2Service.updateById(updateArticleV2);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章表-通过id删除")
    @ApiOperation(value = "文章表-通过id删除", notes = "文章表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        cmsArticleV2Service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文章表-批量删除")
    @ApiOperation(value = "文章表-批量删除", notes = "文章表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cmsArticleV2Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章表-通过id查询")
    @ApiOperation(value = "文章表-通过id查询", notes = "文章表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        CmsArticleV2 cmsArticleV2 = cmsArticleV2Service.getById(id);
        return Result.ok(cmsArticleV2);
    }

    //===========================以下是子表信息操作相关API====================================

    /**
     * 通过主表id查询文章评论
     *
     * @param cmsArticleV2Comment
     * @return
     */
    @AutoLog(value = "文章评论-通过主表id查询")
    @ApiOperation(value = "文章评论-通过主表id查询", notes = "文章评论-通过主表id查询")
    @GetMapping(value = "/listCmsArticleV2CommentByMainId")
    public Result<?> listCmsArticleV2CommentByMainId(CmsArticleV2Comment cmsArticleV2Comment,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        if (cmsArticleV2Comment.getArticleId() != null) {
            QueryWrapper<CmsArticleV2Comment> queryWrapper = QueryGenerator.initQueryWrapper(cmsArticleV2Comment, req.getParameterMap());
            Page<CmsArticleV2Comment> page = new Page<CmsArticleV2Comment>(pageNo, pageSize);
            IPage<CmsArticleV2Comment> pageList = cmsArticleV2CommentService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.error("没有选择记录");
        }
    }

    /**
     * 添加文章评论
     *
     * @param cmsArticleV2Comment
     * @return
     */
    @AutoLog(value = "文章评论-添加")
    @ApiOperation(value = "文章评论-添加", notes = "文章评论-添加")
    @PostMapping(value = "/addCmsArticleV2Comment")
    public Result<?> addCmsArticleV2Comment(@RequestBody CmsArticleV2Comment cmsArticleV2Comment) {
        cmsArticleV2CommentService.save(cmsArticleV2Comment);
        return Result.ok("添加文章评论成功！");
    }

    /**
     * 编辑文章评论
     *
     * @param cmsArticleV2Comment
     * @return
     */
    @AutoLog(value = "文章评论-编辑")
    @ApiOperation(value = "文章评论-编辑", notes = "文章评论-编辑")
    @PutMapping("/editCmsArticleV2Comment")
    public Result<?> editCmsArticleV2Comment(@RequestBody CmsArticleV2Comment cmsArticleV2Comment) {
        cmsArticleV2CommentService.updateById(cmsArticleV2Comment);
        return Result.ok("编辑文章评论成功！");
    }

    /**
     * 审核
     *
     * @param cmsArticleV2Comment
     * @return
     */
    @AutoLog(value = "文章回复表-审核")
    @ApiOperation(value="文章回复表-审核", notes="文章回复表-审核")
    @PutMapping(value = "/editCmsArticleV2CommentStatus")
    public Result<?> editCmsArticleV2CommentStatus(@RequestBody CmsArticleV2Comment cmsArticleV2Comment) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cmsArticleV2Comment.setReviewer(sysUser.getUsername());
        cmsArticleV2Comment.setReviewTime(new Date());
        cmsArticleV2CommentService.updateById(cmsArticleV2Comment);
        return Result.ok("编辑成功!");
    }


    /**
     * 通过id删除文章评论
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章评论-通过id删除")
    @ApiOperation(value = "文章评论-通过id删除", notes = "文章评论-通过id删除")
    @DeleteMapping(value = "/deleteCmsArticleV2Comment")
    public Result<?> deleteCmsArticleV2Comment(@RequestParam(name = "id", required = true) Long id) {
        cmsArticleV2CommentService.removeById(id);
        return Result.ok("删除文章评论成功！");
    }

    /**
     * 批量删除文章评论
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文章评论-批量删除")
    @ApiOperation(value = "文章评论-批量删除", notes = "文章评论-批量删除")
    @DeleteMapping(value = "/deleteBatchCmsArticleV2Comment")
    public Result<?> deleteBatchCmsArticleV2Comment(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        }
        this.cmsArticleV2CommentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");

    }

    //-------------评价回复API--------------------------------------------------

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "评价回复-通过评论ID查询回复列表")
    @ApiOperation(value="评价回复-通过评论ID查询回复列表", notes="评价回复-通过评论ID查询回复列表")
    @GetMapping(value = "/queryListByCommentId")
    public Result<?> queryListByCommentId(@RequestParam(name="id",required=true) Long id) {
        Result<List<CmsArticleV2CommentReply>> result = new Result<>();
        LambdaQueryWrapper<CmsArticleV2CommentReply> query = new LambdaQueryWrapper<>();
        query.eq(CmsArticleV2CommentReply::getCommentId,id);
        query.orderByDesc(CmsArticleV2CommentReply::getCreateTime);
        List<CmsArticleV2CommentReply> list = cmsArticleV2CommentReplyService.list(query);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    /**
     * 添加
     *
     * @param cmsArticleV2CommentReply
     * @return
     */
    @AutoLog(value = "文章回复表-添加")
    @ApiOperation(value="文章回复表-添加", notes="文章回复表-添加")
    @PostMapping(value = "/addCmsArticleV2CommentReply")
    public Result<?> addCmsArticleV2CommentReply(@RequestBody CmsArticleV2CommentReply cmsArticleV2CommentReply) {
        CmsArticleV2Comment articleV2Comment = cmsArticleV2CommentService.getById(cmsArticleV2CommentReply.getCommentId());
        cmsArticleV2CommentReply.setArticleId(articleV2Comment.getArticleId());
        cmsArticleV2CommentReply.setMemberNickname("飞科客服");
        cmsArticleV2CommentReply.setMemberIcon("https://pic.flyco.net.cn/mall/newfeike.jpg");
        cmsArticleV2CommentReply.setMemberType(9);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cmsArticleV2CommentReply.setReviewStatus(2);
        cmsArticleV2CommentReply.setReviewer(sysUser.getUsername());
        cmsArticleV2CommentReply.setReviewTime(new Date());
        cmsArticleV2CommentReplyService.save(cmsArticleV2CommentReply);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cmsArticleV2CommentReply
     * @return
     */
    @AutoLog(value = "文章回复表-编辑")
    @ApiOperation(value="文章回复表-编辑", notes="文章回复表-编辑")
    @PutMapping(value = "/editCmsArticleV2CommentReply")
    public Result<?> editCmsArticleV2CommentReply(@RequestBody CmsArticleV2CommentReply cmsArticleV2CommentReply) {
        cmsArticleV2CommentReplyService.updateById(cmsArticleV2CommentReply);
        return Result.ok("编辑成功!");
    }

    /**
     * 审核
     *
     * @param cmsArticleV2CommentReply
     * @return
     */
    @AutoLog(value = "文章回复表-审核")
    @ApiOperation(value="文章回复表-审核", notes="文章回复表-审核")
    @PutMapping(value = "/editCmsArticleV2CommentReplyStatus")
    public Result<?> editCmsArticleV2CommentReplyStatus(@RequestBody CmsArticleV2CommentReply cmsArticleV2CommentReply) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cmsArticleV2CommentReply.setReviewer(sysUser.getUsername());
        cmsArticleV2CommentReply.setReviewTime(new Date());
        cmsArticleV2CommentReplyService.updateById(cmsArticleV2CommentReply);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章回复表-通过id删除")
    @ApiOperation(value="文章回复表-通过id删除", notes="文章回复表-通过id删除")
    @DeleteMapping(value = "/deleteCmsArticleV2CommentReply")
    public Result<?> deleteCmsArticleV2CommentReply(@RequestParam(name="id",required=true) String id) {
        cmsArticleV2CommentReplyService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文章回复表-批量删除")
    @ApiOperation(value="文章回复表-批量删除", notes="文章回复表-批量删除")
    @DeleteMapping(value = "/deleteBatchCmsArticleV2CommentReply")
    public Result<?> deleteBatchCmsArticleV2CommentReply(@RequestParam(name="ids",required=true) String ids) {
        this.cmsArticleV2CommentReplyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cmsArticleV2
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CmsArticleV2 cmsArticleV2) {
        return super.exportXls(request, cmsArticleV2, CmsArticleV2.class, "文章表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CmsArticleV2.class);
    }

}
